package chapter17;

import java.util.Random;
import java.util.concurrent.*;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/2/24 13:20
 */
public class BasicTaskDemo {
    public static void main(String[] args) {
        ExecutorService executor = Executors.newSingleThreadExecutor();
        Future<Integer> future = executor.submit(new Task());
        int time;
        //模拟执行任务
        try {
            TimeUnit.MILLISECONDS.sleep(100);
            time = future.get();
            System.out.println("sleep time:" + time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        executor.shutdown();
    }

    static class Task implements Callable<Integer> {

        @Override
        public Integer call() throws Exception {
            int sleepTime = new Random().nextInt(1000);
            TimeUnit.MILLISECONDS.sleep(sleepTime);
            return sleepTime;
        }
    }
}
